package com.example.server.controller;

import com.example.server.entity.Store;
import com.example.server.redis.RedisService;
import com.example.server.service.StoreService;
import com.example.server.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("store")
public class StoreController extends BaseController{
    private StoreService storeService;
    @Autowired
    private RedisService redisService;

    public StoreController(StoreService storeService) {
        this.storeService = storeService;
    }

    @PostMapping("create")
    private Store create(@RequestBody Store store){
        storeService.create(store);
        return queryById();
    }

    @PostMapping("queryByAccountAndPassword")
    private Store login(@RequestBody Store store) throws IOException {
        Store store1 = storeService.queryOne("queryByAccountAndPassword",store);
        if (store1 != null){
            int id = store1.getId();
            System.out.println(id);
            redisService.setString("storeId", String.valueOf(id));
            System.out.println(redisService.getSetString("storeId"));
            return store1;
        }else {
            return null;
        }

    }
//    private String login(@RequestBody Store store){
//        store = storeService.queryOne("queryByAccountAndPassword",store);
//        if(store != null){
//            String token = Util.generate(String.valueOf(store.getId()));
//            System.out.println(token);
//            System.out.println("=====================" + Util.parse(token));
//            return token;
//        }
//        return null;
//    }

    @GetMapping("queryById")
    private Store queryById(){
        int id = (int) session.getAttribute("storeId");
        Store store = storeService.queryOne("queryById",id);
        return store;
    }

    @PutMapping("modify")
    private int modify(@RequestBody Store store){
        int id  = (int) session.getAttribute("storeId");
        store.setId(id);
        return storeService.modify(store);
    }

    //更改密码
    @PutMapping("modifyPassword")
    private int modifyPassword(@RequestBody Store store){
        int id  = (int) session.getAttribute("storeId");
        store.setId(id);
        return storeService.modifyOne("modifyPassword",store);
    }

    @GetMapping("queryAll")
    private List<Store> queryAll(){
        return storeService.queryAll();
    }

    //退出登录
    @GetMapping("/logout")
    public String logout() {
        // 获取当前请求的Session对象
        session = request.getSession(false);
        if (session != null) {
            // 销毁Session
            session.invalidate();
        }
        return "success";
    }

    @GetMapping("/yz")
    private String yz(){
        if(session.getAttribute("storeId") == null){
            return "已退出";
        }else {
            return "";
        }
    }

    //注销
    @DeleteMapping("remove")
    private int removeById(){
        int id = (int) session.getAttribute("storeId");
        return storeService.removeById(id);
    }
}
